package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.IMetaOrgDao;
import com.qymerge.manage.base.dao.IMetaPeriodDao;
import com.qymerge.manage.base.ro.MetaOrgRO;
import com.qymerge.manage.base.ro.OrganizationalStructureRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 *<p>Title: MetaOrgDaoImpl</p>
 *<p>Description: 组织架构</p>
 *
 * @author Xiao
 * @date 2025/5/2 01:23
 * @version 1.0.0
 */
@Repository
public class MetaOrgDaoImpl extends BaseDao implements IMetaOrgDao {


    private String dwd_MCRS_MDM_meta_Org = "SELECT Id, Name, NodeLevel, Flag, ParentId, OrgVersionId, sort FROM dwd_MCRS_MDM_meta_Org WHERE 1=1 ";

    @Override
    public List list(MetaOrgRO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = dwd_MCRS_MDM_meta_Org;
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getId() > 0) {
            sql += " and Id=:Id";
            paramMap.put("Id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getName())) {
            sql += " and Name like :Name";
            paramMap.put("Name", "%" + ro.getName() + "%");
        }

        if (ro.getFlag() > -1) {
            sql += " and Flag=:Flag";
            paramMap.put("Flag", ro.getFlag());
        }

        // 分页数据获取
        sql += " order by Id desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return getNamedParameterJdbcTemplate().query(sql, paramMap, new BeanPropertyRowMapper<MetaOrgRO>(MetaOrgRO.class));
    }

    @Override
    public int total(MetaOrgRO ro) {
        String sql = "select count(1) from dwd_MCRS_MDM_meta_Org where 1=1";
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getId() > 0) {
            sql += " and Id=:Id";
            paramMap.put("Id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getName())) {
            sql += " and Name like :Name";
            paramMap.put("Name", "%" + ro.getName() + "%");
        }

        if (ro.getFlag() > -1) {
            sql += " and Flag=:Flag";
            paramMap.put("Flag", ro.getFlag());
        }

        return getNamedParameterJdbcTemplate().queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public List<MetaOrgRO> findAll(MetaOrgRO ro) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_meta_Org);
        Map<String, Object> paramMap = new HashMap<String, Object>();

        sql.append(" and OrgVersionId=:OrgVersionId");
        paramMap.put("OrgVersionId", ro.getOrgVersionId());

        sql.append(" order by sort desc");
        return getNamedParameterJdbcTemplate().query(sql.toString(), paramMap, new BeanPropertyRowMapper<MetaOrgRO>(MetaOrgRO.class));
    }

    @Override
    public MetaOrgRO findOne(Long id) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_meta_Org);
        sql.append("and Id=?");
        RowMapper<MetaOrgRO> rowMapper = new BeanPropertyRowMapper<MetaOrgRO>(MetaOrgRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public List<MetaOrgRO> findAllByParentId(Long id) {
        NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(this.getJdbcTemplate());
        String sql = "select * from dwd_MCRS_MDM_meta_Org where ParentId in(:ids)";
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("ids", id);
        return jdbc.query(sql, paramMap, new BeanPropertyRowMapper<MetaOrgRO>(MetaOrgRO.class));
    }

    @Override
    public int insert(MetaOrgRO ro) {
        StringBuilder sql = new StringBuilder("insert into dwd_MCRS_MDM_meta_Org(Name, Flag, NodeLevel, ParentId, sort, OrgVersionId) values(?,?,?,?,?,?)");

        List params = new ArrayList();
        params.add(ro.getName());
        params.add(ro.getFlag());
        params.add(ro.getNodeLevel());
        params.add(ro.getParentId());
        params.add(ro.getSort());

        params.add(ro.getOrgVersionId());

        return getJdbcTemplate().update(sql.toString(), params.toArray());
    }

    @Override
    public int update(MetaOrgRO ro) {
        StringBuilder sql = new StringBuilder("update dwd_MCRS_MDM_meta_Org set Name=:Name, Flag=:Flag, NodeLevel=:NodeLevel, sort=:sort where Id=:Id ");

        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("Name", ro.getName());
        paramsMap.put("Flag", ro.getFlag());
        paramsMap.put("NodeLevel", ro.getNodeLevel());
        paramsMap.put("sort", ro.getSort());

        paramsMap.put("Id", ro.getId());
        return getNamedParameterJdbcTemplate().update(sql.toString(), paramsMap);
    }

    @Override
    public int remove(long Id) {
        // 删除
        String sql="delete from dwd_MCRS_MDM_meta_Org where Id=?";
        return getJdbcTemplate().update(sql, Id);
    }


}
